<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Axios操作</title>
    <script type="text/javascript" src="js/vue.js"></script>
    <script type="text/javascript" src="js/axios.min.js"></script>
</head>
<body>
<div id="d1">
    用户名:<input type="text" v-model="user.username"/><br/>
    密码:<input type="password" v-model="user.password"/><br/>
    <input type="button" value="登录" @click="login"/>
    <input type="button" value="get请求" @click="loginGet"/>
    <input type="button" value="post请求" @click="loginPost"/>
    <input type="button" value="delete请求" @click="loginDelete"/>
    <input type="button" value="put请求" @click="loginPut"/>
</div>
</body>
<script>
    new Vue({
        el: "#d1",
        data:{
            user:{
                id:1,
                username:"",
                password:""
            }
        },methods:{
            login(){
                axios.get("http://localhost:8080/users/f1").then(result =>{
                    console.log(result.data);
                });
            },
            loginGet(){
                axios.get("http://localhost:8080/users/f2",{params: this.user}).then(result =>{
                    console.log(result.data);
                });
            },
            loginPost(){
                // 在使用post请求的时候,不能使用payload的数据格式进行传输
                // 必须使用表单数据
                // Axios中提供一个URLSearchParams对象,用于表单数据的传输
                // 该对象类似于Map,使用key-value的格式进行数据的封装
                let params = new URLSearchParams();
                params.append("id",this.user.id);
                params.append("username",this.user.username);
                params.append("password",this.user.password);
                axios.post("http://localhost:8080/users/f2",params).then(result =>{
                    console.log(result.data);
                });
            },
            loginDelete(){
                axios.delete("http://localhost:8080/users/f2",{params: this.user}).then(result =>{
                    console.log(result.data);
                });
            },
            loginPut(){
                let params = new URLSearchParams();
                params.append("id",this.user.id);
                params.append("username",this.user.username);
                params.append("password",this.user.password);
                axios.put("http://localhost:8080/users/f2",params).then(result =>{
                    console.log(result.data);
                });
            }
        },
        created(){
            this.login();
        }
    })
</script>
</html>